package servlet;

import psl.*;

import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import java.io.*;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: phiteshkumar
 * Date: Feb 24, 2007
 * Time: 12:35:02 PM
 */
public class ROController extends HttpServlet
{
    public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doPost (request, response);
    }

    public void doPost (HttpServletRequest foRequest, HttpServletResponse foResponse) throws ServletException, IOException
    {
        HashMap lhRequest = parseRequest (foRequest);
        HttpSession loHttpSession = foRequest.getSession (true);
        UserSession loUserSession = (UserSession) loHttpSession.getAttribute ("UserSession");
		String lsSessionID = loHttpSession.getId ();
        if (loUserSession == null)
        {
			System.out.println ("Session Id Is: " + lsSessionID);
            loUserSession = new UserSession ();
            loUserSession.msSessionId = lsSessionID;
            loHttpSession.setAttribute ("UserSession", loUserSession);
        }
        else
        {
			if(lsSessionID.equals(loUserSession.msSessionId))
			{
				loUserSession.processRequest (lhRequest);

				BasePage loBasePage = loUserSession.getCurrentPage ();
				String lsViewPage = loBasePage == null ? "MaineFrame.jsp" : loBasePage.getViewPage ();
				foRequest.getRequestDispatcher (lsViewPage).forward (foRequest, foResponse);
			}else
			{

			}
		}

    }

    public HashMap parseRequest (HttpServletRequest foRequest)
    {
        HashMap lhRequest = new HashMap ();
        Object[] loKeySet = foRequest.getParameterMap ().keySet ().toArray ();
        for (int liCtr = 0; liCtr < loKeySet.length; liCtr++)
        {
            String lsKey = (String) loKeySet[liCtr];
            String lsValue = foRequest.getParameter (lsKey);
            if (lsValue != null && !lsValue.equals (""))
            {
                lhRequest.put (lsKey, lsValue);
            }
        }
        String lsAction = (String) lhRequest.get ("action_fld");
        String[] laAction = CommonServices.stringToArray (lsAction, AppConstants.DELIM_MAIN + "");
        lhRequest.put ("action_fld", laAction);
        return lhRequest;
    }
 }
